﻿

namespace Vanglen.YDLMSCP
{
    using System;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Ink;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using SQLiteClient;
    using System.Collections.Generic;
    using System.IO.IsolatedStorage;
    public class SQLiteHelper : IDisposable
    {
        //private string fileName = "yidalimeishi.db";
        private string fileName = "db/sp.db";
        private SQLiteConnection db = null;

        public SQLiteHelper()
        {
            Open();
        }

        ~SQLiteHelper()
        {
            Close();
        }

        private void Open()
        {
            if (db == null)
            {
                db = new SQLiteConnection(fileName);
                db.Open();
            }
        }

        public void Close()
        {
            if (db != null)
            {
                db.Dispose();
                db = null;
            }
        }

        public IEnumerable<T> ExecuteQuery<T>(string sql) where T : new()
        {
            SQLiteCommand cmd = db.CreateCommand(sql);
            return cmd.ExecuteQuery<T>();
        }

        public int ExecuteNonQuery(string sql)
        {
            SQLiteCommand cmd = db.CreateCommand(sql);
            int i = cmd.ExecuteNonQuery();
            return i;
        }

        public int ExecuteNonQuery<T>(string sql, T t)
        {
            SQLiteCommand cmd = db.CreateCommand(sql);
            int i = cmd.ExecuteNonQuery(t);
            return i;
        }

        public void Dispose()
        {
            Close();
        }
    }
}
